<?php

namespace app\common\traits;

use think\exception\HttpResponseException;

/**
 * Trait JumpTrait
 * @package app\common\traits
 */
trait JumpTrait
{

    /**
     * 操作成功
     * @access protected
     * @param mixed $msg 提示信息
     * @param mixed $data 返回的数据
     * @throws HttpResponseException
     */
    protected function success($data = [], $msg = '')
    {
        $this->result($data, 1, $msg ?: 'Success');
    }

    /**
     * 操作错误
     * @access protected
     * @param string $msg 提示信息
     * @return void
     * @throws HttpResponseException
     */
    protected function error($msg = '')
    {
        $this->result([], 0, $msg);
    }

    /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed $data 要返回的数据
     * @param int $code 返回的 code
     * @param string $msg 提示信息
     * @throws HttpResponseException
     */
    protected function result($data, $code = 1, $msg = '')
    {
        $result   = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ];
        throw new HttpResponseException(json($result));
    }
}
